package cn.meizhu.user.service.impl;

import cn.meizhu.base.exception.Exceptions;
import cn.meizhu.base.service.impl.BaseServiceImpl;
import cn.meizhu.user.domain.LoginInfo;
import cn.meizhu.user.domain.User;
import cn.meizhu.user.domain.tro.UserDto;
import cn.meizhu.user.mapper.LoginInfoMapper;
import cn.meizhu.user.mapper.UserMapper;
import cn.meizhu.user.service.IUserService;
import cn.meizhu.util.MD5Utils;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }



    @Override
    @Transactional
    public void register(UserDto userDto) throws Exception {
        //验证数据
        verifyData(userDto);
        //把userDto转为User对象
        User user = userDtoToUser(userDto);
        //把User转为loginInfo对象
        LoginInfo loginInfo = userToLoginInfo(user);
        loginInfoMapper.save(loginInfo);
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);
    }



    /**
     *  封装loginInfo
     * @param user
     * @return
     */
    private LoginInfo userToLoginInfo(User user){
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo);
        loginInfo.setType(1);
        loginInfo.setDisable(user.getState());
        return loginInfo;
    }


    /**
     * UserDto转为user对象
     * @param userDto
     * @return
     */
    private User userDtoToUser(UserDto userDto){
        User user = new User();
        user.setUsername("p_"+userDto.getPhone());
        user.setPhone(userDto.getPhone());
        //设置密钥加盐
        String s = UUID.randomUUID().toString();

        String byMd5 = MD5Utils.encrypByMd5(userDto.getPassword() + s);

        user.setSalt(s);
        user.setPassword(byMd5);
        user.setState(1);
        user.setCreatetime(new Date());
        return user;
    }

    /**
     * 验证数据
     * @param userDto
     * @throws Exceptions
     */
    private void verifyData(UserDto userDto) throws Exceptions {
        if (StringUtils.isNullOrEmpty(userDto.getPhone())){
            throw new Exceptions("手机号不能为空");
        }
        if (StringUtils.isNullOrEmpty(userDto.getCode())){
            throw new Exceptions("验证码不能为空");
        }
        if (StringUtils.isNullOrEmpty(userDto.getPassword())){
            throw new Exceptions("密码不能为空");
        }
        if(StringUtils.isNullOrEmpty(userDto.getPasswordRepeat())){
            throw new Exceptions("两次密码输入不一致，请重新输入");
        }
        String opsForValue = (String) redisTemplate.opsForValue().get(userDto.getPhone() + ":" + userDto.getCodeType());
        if(opsForValue == null){
            throw new Exceptions("验证码无效");
        }
        String code = opsForValue.split(":")[0];
        if (!userDto.getCode().equals(code)){
            throw new Exceptions("验证码错误，请重新输入");
        }
    }

}
